package com.junlong0716.enjoylearning.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.Gravity
import android.view.WindowManager
import com.blankj.utilcode.util.ServiceUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.service.TimerService
import kotlinx.android.synthetic.main.dialog_bottom_timer_layout.*


/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 下午8:33 2018/6/9
 *@modified by:
 */
class BottomTimerDialog(context: Context, onClickListListener: OnClickListListener) : Dialog(context, R.style.down_dialog) {
    private val mContext = context
    private val mOnClickListListener = onClickListListener
    private lateinit var mListData: ArrayList<TimerEntity>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_bottom_timer_layout)

        mListData = ArrayList()

        if (ServiceUtils.isServiceRunning(TimerService::class.java)) {
            mListData.add(TimerEntity("取消定时关闭", 0))
        }

        mListData.add(TimerEntity("15分钟后关闭", 900))
        mListData.add(TimerEntity("30分钟后关闭", 1800))
        mListData.add(TimerEntity("1小时后关闭", 3600))
        mListData.add(TimerEntity("播放完本条", 1))

        rv_list.layoutManager = LinearLayoutManager(mContext)
        val timerAdapter = TimerAdapter(mListData, R.layout.item_timer_close)
        rv_list.adapter = timerAdapter

        timerAdapter.setOnItemClickListener { _, _, position ->
            mOnClickListListener.onListClickListener(this@BottomTimerDialog, mListData[position].timeName, mListData[position].timeCount)
        }

        val window = this.window
        window!!.setGravity(Gravity.BOTTOM)
        val params = window.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.attributes = params
    }

    interface OnClickListListener {
        fun onListClickListener(dialog: BottomTimerDialog, timeName: String, time: Long)
    }

    inner class TimerEntity(var timeName: String, var timeCount: Long)

    inner class TimerAdapter(data: ArrayList<TimerEntity>, resLayout: Int) : BaseQuickAdapter<TimerEntity, BaseViewHolder>(resLayout, data) {
        override fun convert(helper: BaseViewHolder?, item: TimerEntity?) {
            helper!!.setText(R.id.tv_timer_close, item!!.timeName)
        }
    }
}